package com.gengral.bussiness.sys.user.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import com.gengral.commons.utils.ResponseMsgUtil;
import com.gengral.commons.msg.BaseResponse;
import com.gengral.commons.annotation.SysLog;
import  com.gengral.bussiness.sys.user.service.UserService;
import  com.gengral.bussiness.sys.user.entity.User;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sht
 * @date 2020-04-14
 */
@RestController
@CrossOrigin
@Api(value = "userCRUD接口")
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取user根据id",notes="")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    @GetMapping("/getById")
    @SysLog(description = "获取user根据id")
    public BaseResponse user(@RequestParam(value = "id") Long id) throws Exception {
        return ResponseMsgUtil.success(userService.getUserById(id));
    }

    @ApiOperation(value = "获取user列表",notes="")
    @ApiImplicitParam(name = "user", value = "user实体中的参数", dataType ="User")
    @GetMapping("/list")
    @SysLog(description = "获取user列表")
    public BaseResponse userList(@RequestParam(required = false) User user) throws Exception {
        return ResponseMsgUtil.success(userService.listUser(user));
    }

    @ApiOperation(value = "分页获取user列表",notes="")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "user", value = "user实体中的参数", dataType="User"),
        @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "String"),
        @ApiImplicitParam(name = "pageSize", value = "每页数据条数", dataType = "String"),
    })
    @GetMapping("/page")
    @SysLog(description = "分页获取user列表")
    public BaseResponse userPage(@RequestParam(required = false) User user,
                                        @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
        return ResponseMsgUtil.success(userService.pageUser(user, pageNo, pageSize));
    }

    @ApiOperation(value = "添加user",notes="新增一条user")
    @ApiImplicitParam(name = "user", value = "user实体中的参数", required = true, dataType = "User")
    @PostMapping("/save")
    @SysLog(description = "添加user")
    public BaseResponse userAdd(@RequestBody User user) throws Exception {
        return ResponseMsgUtil.success(userService.addUser(user));
    }

    @ApiOperation(value = "修改user",notes="根据id修改sys_user")
    @ApiImplicitParam(name = "user", value = "user实体", required = true, dataType = "User")
    @PutMapping("/update")
    @SysLog(description = "修改user")
    public BaseResponse userUpdate(@RequestBody User user) throws Exception {
        return ResponseMsgUtil.success(userService.updateUser(user));
    }
    @ApiOperation(value = "物理删除user",notes="根据id物理删除user")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    @DeleteMapping("/delete/{id}")
    @SysLog(description = "物理删除user")
    public BaseResponse userDelete(@PathVariable(name = "id") Long id) throws Exception {
        return ResponseMsgUtil.success(userService.deleteUserById(id));
    }

    @ApiOperation(value = "逻辑删除user",notes="根据id逻辑删除user")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    @PostMapping("/removeById")
    @SysLog(description = "逻辑删除user")
    public BaseResponse userRemove(@RequestParam(value = "id") Long id) throws Exception {
        return ResponseMsgUtil.success(userService.removeUserById(id));
    }
}
